package com.cnb.telepathytalk.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cnb.telepathytalk.R;
import com.cnb.telepathytalk.dto.RoomListDTO;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST01;
import com.cnb.telepathytalk.util.LazyImageLoader;

public class RoomListAdatper extends BaseAdapter {
	private Context context;
	private ArrayList<RoomListDTO> list;
	private HashMap<String, String> picIndexKey;
	private int layoutResId;
	private LayoutInflater inflater;

	private LazyImageLoader imageLoader;

	public RoomListAdatper(Context context) {
		super();
		this.context = context;
		this.list = new ArrayList<RoomListDTO>();
		this.picIndexKey = new HashMap<String, String>();
		this.layoutResId = R.layout.list_item_room;
		this.inflater = LayoutInflater.from(context);
		this.imageLoader = new LazyImageLoader(context, R.drawable.profile_talk, 50, LazyImageLoader.SET_IMAGE);
	}

	public HashMap<String, String> getPicIndexKey() {
		return picIndexKey;
	}

	public ArrayList<RoomListDTO> getList() {
		return list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public RoomListDTO getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		try {
			Holder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(layoutResId, null);
				holder = new Holder(convertView);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}

			RoomListDTO item = list.get(position);
			holder.getTitle().setText(item.getTitle());
			holder.getLastMessage().setText(item.getLastMsg());
			holder.getCreateTime().setText(getDate(item.getCreateTime()));

			if (item.getNonReadNum() == 0) {
				holder.getNonReadNum().setVisibility(View.GONE);
			} else {
				holder.getNonReadNum().setVisibility(View.VISIBLE);
				holder.getNonReadNum().setText("" + item.getNonReadNum());
			}

			switch (item.getType()) {
			case Packet_PT8_ST01.TYPE_FRIEND_1:
			case Packet_PT8_ST01.TYPE_TELE_1:
				imageLoader.setDefaultRsc(R.drawable.profile_talk);
				break;

			case Packet_PT8_ST01.TYPE_FRIEND_N:
			case Packet_PT8_ST01.TYPE_TELE_N:
				imageLoader.setDefaultRsc(R.drawable.profile_group);
				break;
			}

			imageLoader.DisplayImage(picIndexKey.get("" + item.getFirstUcode()), holder.getProfile());

		} catch (Exception e) {
			Log.e("RoomListAdatper", e.toString());
			e.printStackTrace();
		}

		return convertView;
	}

	private String getDate(String time) {
		try {
			Calendar createDay = Calendar.getInstance();
			Calendar nowDay = Calendar.getInstance();

			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
			createDay.setTime(sdf.parse(time));

			if (createDay.get(Calendar.DAY_OF_MONTH) == nowDay.get(Calendar.DAY_OF_MONTH)) {
				String am_pm = createDay.get(Calendar.AM_PM) == Calendar.PM ? context.getString(R.string.chat_pm) : context
						.getString(R.string.chat_am);
				int hour = createDay.get(Calendar.HOUR);
				int minute = createDay.get(Calendar.MINUTE);
				return String.format(Locale.getDefault(), "%s %02d : %02d", am_pm, hour, minute);
			} else {
				int year = createDay.get(Calendar.YEAR);
				int month = createDay.get(Calendar.MONTH) + 1;
				int day = createDay.get(Calendar.DAY_OF_MONTH);
				return String.format(Locale.getDefault(), "%04d. %02d. %02d", year, month, day);
			}
		} catch (ParseException e) {
		}
		return time;
	}

	private class Holder {
		private View baseView;

		private ImageView profile;
		private TextView title;
		private TextView lastMessage;
		private TextView createTime;
		private TextView nonReadNum;

		public Holder(View view) {
			super();
			baseView = view;
		}

		public ImageView getProfile() {
			if (profile == null)
				profile = (ImageView) baseView.findViewById(R.id.profileImage);
			return profile;
		}

		public TextView getTitle() {
			if (title == null)
				title = (TextView) baseView.findViewById(R.id.titleText);
			return title;
		}

		public TextView getLastMessage() {
			if (lastMessage == null)
				lastMessage = (TextView) baseView.findViewById(R.id.lastMsg);
			return lastMessage;
		}

		public TextView getCreateTime() {
			if (createTime == null)
				createTime = (TextView) baseView.findViewById(R.id.createTime);
			return createTime;
		}

		public TextView getNonReadNum() {
			if (nonReadNum == null)
				nonReadNum = (TextView) baseView.findViewById(R.id.nonReadNum);
			return nonReadNum;
		}
	}
}
